package com.inkegz.message.repo.action

import android.util.Log
import com.inkegz.message.repo.base.ObjectBoxAction
import com.inkegz.message.repo.base.ObjectBoxState
import com.inkegz.message.utils.conversationBox
import com.inkegz.message.utils.messageBox
import com.inkegz.message.utils.queryConversation

/**
 * 删除 会话 操作
 */
class RemoveConversationAction(
    private val conversationKey: String
): ObjectBoxAction() {
    override suspend fun opt(state: ObjectBoxState): ObjectBoxState {
        val conversation = state.queryConversation(conversationKey)
            ?: run {
                Log.e("ImRepo", "can not find conversation when remove conversation")
                return state
            }
        state.conversationBox().remove(conversation)
        // 可以考虑 toMany 属性 removeFromTargetBox
        state.messageBox().remove(conversation.messages)
        return ObjectBoxState(state.model)
    }
}